//
//  ECMineProfileSelfSignVC.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/17.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECMineProfileSelfSignVC.h"
#import "UITextView+Placeholder.h"
#import "UITextView+KTLimitCounter.h"
#import "CYDataHelper.h"

@interface ECMineProfileSelfSignVC (){
    UITextView *introTV;
}

@end

@implementation ECMineProfileSelfSignVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个性签名";
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(getDataWithURL)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    [self createViewUI];
}

-(void)createViewUI{
    
    UIView *topBaseView = [[UIView alloc]init];
    [self.view addSubview:topBaseView];
    topBaseView.backgroundColor = [UIColor whiteColor];
    
    introTV = [[UITextView alloc]init];
    [topBaseView addSubview:introTV];
    introTV.textColor = [UIColor colorWithHexString:@"#333333"];
    
    //在设置placeholder之前设置文本内容,否者会引起字数限制刷新无效
    if (![CYDataHelper isNull:self.selfSign]) {
        introTV.text = self.selfSign;
        [introTV becomeFirstResponder];
    }
    
    introTV.placeholder = @"说点什么吧";
    introTV.font = [UIFont systemFontOfSize:15*kPercentage];
    introTV.kt_limitCount = 30;//限制字数
    introTV.kt_labMargin = 0;//右边距
    introTV.kt_labHeight = 35;//高度
    
    UIView *lineView = [[UIView alloc]init];
    [topBaseView addSubview:lineView];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    
    WeakSelf(weakSelf);
    [topBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_top);
        make.width.equalTo(weakSelf.view.mas_width);
        make.height.equalTo(@(kProportion_Height(584)));
    }];
    
    [introTV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBaseView.mas_top);
        make.left.equalTo(topBaseView.mas_left).offset(kProportion_Width(40));
        make.right.equalTo(topBaseView.mas_right).offset(-kProportion_Width(40));
        make.bottom.equalTo(topBaseView.mas_bottom);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(topBaseView.mas_bottom);
        make.width.equalTo(topBaseView.mas_width);
        make.height.equalTo(@1);
    }];
}

-(void)getDataWithURL{
    DLog(@"%@",[[UserInfoManager shareUser] token]);
    [HWHttpTool requestWithMethod:@"POST"
                              url:URL_POST_MeSetting
                           params:@{@"value":introTV.text,
                                    @"field":@"signature"
                                    }
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              DLog(@"%@",json);
                              [MBProgressHUD showTipMessageInWindow:@"个性签名更新成功"];
                              [[UserInfoManager shareUser]setSignAture:introTV.text];//更新本地个签
                              [self.navigationController popViewControllerAnimated:YES];
                          }
                          failure:^(NSError *error) {
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              DLog(@"%@",str);
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:@"个性签名更新失败"];
                              }
                          }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
